﻿namespace DoVuiKinhThanh
{
    using System;
    using System.IO;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;

    using DoVuiKinhThanh.Domain;

    using Xceed.Wpf.Toolkit;

    /// <summary>
    /// Interaction logic for QuizControl.xaml
    /// </summary>
    public partial class QuizControl : UserControl
    {
        #region IFields

        private bool m_isReadOnly;

        #endregion
        #region IConstructors

        public QuizControl()
        {
            InitializeComponent();
            SetQuizTextBoxFormatBar();
            var defaultImagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory , @"Images\NoImage.png");
            if (File.Exists(defaultImagePath)) {
                UpdateDescriptionImage(defaultImagePath);
            }
        }

        #endregion
        #region IMethods

        public void BindQuizData(Quiz quiz)
        {
            m_selectedQuizContainer.DataContext = quiz;
        }

        private void OnBtnBrowseImageClick(object sender, RoutedEventArgs e)
        {
            // Create OpenFileDialog
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            // Set filter for file extension and default file extension
            dlg.DefaultExt = ".png";
            dlg.Filter = "Image files|*.png;*.jpg;*.bmp";

            // Display OpenFileDialog by calling ShowDialog method
            var result = dlg.ShowDialog();

            // Get the selected file name and display in a TextBox
            if (result.Value) {
                // Open document
                string filename = dlg.FileName;
                m_txtImageFilePath.Text = filename;
                UpdateDescriptionImage(filename);
            }
        }

        private void UpdateDescriptionImage(string filename) {
            BitmapImage src = new BitmapImage();
            src.BeginInit();
            src.UriSource = new Uri(filename);
            src.EndInit();
            m_imgDescription.Source = src;
            m_imgDescription.Stretch = Stretch.Uniform;
        }

        private void SetQuizTextBoxFormatBar()
        {
            if (!m_isReadOnly)
            {
                RichTextBoxFormatBar formatBar = new RichTextBoxFormatBar();
                RichTextBoxFormatBarManager.SetFormatBar(m_txtQuestion, formatBar);
            }
        }

        #endregion
        #region IProperties

        public bool Enabled
        {
            get
            {
                return !IsReadOnly;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return m_isReadOnly;
            }
            set
            {
                m_isReadOnly = value;
                m_txtQuestion.IsReadOnly = value;
                m_gdvAnswers.IsReadOnly = value;
                m_txtImageFilePath.IsReadOnly = value;
                m_btnBrowseImage.IsEnabled = !value;
                SetQuizTextBoxFormatBar();
            }
        }

        public Xceed.Wpf.Toolkit.RichTextBox QuizRichTextBox
        {
            get
            {
                return m_txtQuestion;
            }
        }

        #endregion
    }
}